package com.fyzk.activity.user.userinfo;

import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.fyzk.base.BaseActivity;
import com.fyzk.activity.app.App;
import com.fyzk.activity.app.AppToast;
import com.fyzk.bean.UserInfoResponse;
import com.fyzk.data.preference.MySharedPreferences;
import com.fyzk.net.Commands;
import com.fyzk.net.PublicMethod;
import com.fyzk.utils.AppUtil;
import com.fyzk.utils.Utils;
import com.fyzk.view.TopTitleView;
import com.tyjc.lottery.R;

import org.json.JSONException;
import org.json.JSONObject;
import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.ViewInject;

import http.Common.CommandUtil;
import http.DataUtil.text.TextUtil;
import http.StringResultBean;

/**
 * Created by fanxs11 on 17/6/8.
 * 实名认证
 */
@ContentView(R.layout.ac_authentication)
public class AuthenticationActivity extends BaseActivity {

    @ViewInject(R.id.authentication_topview)
    private TopTitleView topView;
    @ViewInject(R.id.authentication_realname_et)
    private EditText realName_et;
    @ViewInject(R.id.authentication_idnumber_et)
    private EditText idNumber_et;
    @ViewInject(R.id.authentication_infor_phone)
    private TextView inforPhone_tv;
    @ViewInject(R.id.authentication_infor_qq)
    private TextView inforQQ_tv;
    @ViewInject(R.id.tv_commit)
    private TextView commit;

    // 客服电话
//    private String servicePhone;
    // 客服QQ
    private String serviceQQ;

    private UserInfoResponse userInfoResponse;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        userInfoResponse = (UserInfoResponse) getIntent().getSerializableExtra("userinfo");

//        servicePhone = AppUtil.getFormatStr(
//                App.getContext().getResources().getString(R.string.authentication_tishi2),
//                MySharedPreferences.getInstance().getCustomerCalls());

        serviceQQ = AppUtil.getFormatStr(
                App.getContext().getResources().getString(R.string.authentication_tishi3),
                MySharedPreferences.getInstance().getServiceQQ());

        initView();
    }

    private void initView() {
        topView.setOnTitleClickListener(this);
//        topView.setRightTxt("认证");
//        topView.getRightTxt().setBackgroundResource(R.color.touming);
        if (userInfoResponse!=null&&!TextUtil.isEmpty(userInfoResponse.getRealName())) {
            topView.getRightTxt().setVisibility(View.INVISIBLE);
            realName_et.setText(userInfoResponse.getRealName());
            idNumber_et.setText(PublicMethod.tv_yinCang(userInfoResponse.getIdcard(), 3,
                    3));

            realName_et.setEnabled(false);
            idNumber_et.setEnabled(false);
        } else {
            topView.setRightVisible(View.VISIBLE);
            topView.getRightTxt().setGravity(Gravity.RIGHT);
        }

//        inforPhone_tv.setText(servicePhone);
        inforQQ_tv.setText(serviceQQ);

        commit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                commit();
            }
        });
    }

    @Override
    public void onRightClick() {
        commit();
    }

    private void commit() {
        if (TextUtil.isEmpty(realName_et.getText().toString())) {
            AppToast.getToast().show("真实姓名不能为空");
            return;
        }

//        if (!Utils.isChineseStr(realName_et.getText().toString())) {
//            AppToast.getToast().show("真实姓名须为中文");
//            return;
//        }

        if (TextUtil.isEmpty(idNumber_et.getText().toString())) {
            AppToast.getToast().show("身份证号不能为空");
            return;
        }

        if (!Utils.isIDcard18(idNumber_et.getText().toString())) {
            AppToast.getToast().show("身份证号输入不合法");
            idNumber_et.requestFocus();
            idNumber_et.setSelectAllOnFocus(true);
            return;
        }

        requestBindIdNumber();
    }

    /**
     * 绑定身份证
     */
    private void requestBindIdNumber() {
        JSONObject request_json = new JSONObject();
        try {
            request_json.put("realName", realName_et.getText().toString());
            request_json.put("idcard", idNumber_et.getText().toString());
            httpPostJson(CommandUtil.BINDIDCARD, Commands.BINDIDCARD, request_json, false);
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    @Override
    protected void onSuccess(int requestCode, StringResultBean resultBean, String result) {
        super.onSuccess(requestCode, resultBean, result);
        switch (requestCode) {
            case CommandUtil.BINDIDCARD:
                AppToast.getToast().show("认证成功");
                finish();
                break;
        }
    }
}
